package com.ailot.cloud.upms.api.controller;

import cn.hutool.core.util.StrUtil;
import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.upms.api.service.SysRoleMenuService;
import com.ailot.cloud.upms.api.service.SysRoleService;
import com.ailot.cloud.upms.common.entity.SysRole;
import com.ailot.cloud.upms.common.entity.SysRoleMenu;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/role")
public class SysRoleController {
    private final SysRoleService sysRoleService;

    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 通过ID查询角色信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:get')")
    public SysRole getById(@PathVariable String id) {
        return sysRoleService.getById(id);
    }

    /**
     * 添加角色
     *
     * @param sysRole 角色信息
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:add')")
    public Boolean add(@Valid @RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole);
    }

    /**
     * 修改角色
     *
     * @param sysRole 角色信息
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:role:edit')")
    public Result<?> edit(@Valid @RequestBody SysRole sysRole) {
        return Result.success(sysRoleService.updateById(sysRole));
    }

    /**
     * 删除角色
     *
     * @param id 主键id
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:del')")
    public Result delete(@PathVariable String id) {
        return Result.success(sysRoleService.removeById(id));
    }

    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public List<SysRole> getList(SysRole sysRole) {
        return sysRoleService.list(Wrappers.query(sysRole));
    }

    /**
     * 分页查询角色信息
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:role:page')")
    public Page<SysRole> getRolePage(Page<SysRole> page) {
        return sysRoleService.page(page, Wrappers.emptyWrapper());
    }


    /**
     * 更新角色菜单权限
     *
     * @param sysRoleMenu menuId 菜单ID拼成的字符串，每个id之间根据逗号分隔
     * @return ok、false
     */
    @PutMapping("/menu")
    @PreAuthorize("hasAuthority('sys:role:perm')")
    public Result<?> saveRoleMenus(@RequestBody SysRoleMenu sysRoleMenu) {
        String roleId = sysRoleMenu.getRoleId();
        String menuIds = sysRoleMenu.getMenuId();
        if (StrUtil.isBlank(roleId) || StrUtil.isBlank(menuIds)) {
            return Result.fail("缺少特定属性，无法操作");
        }
        if (sysRoleService.judeAdmin(roleId)) {
            return Result.fail("管理员角色不能操作");
        }
        return Result.success(sysRoleMenuService.updateRoleMenus(roleId, menuIds));
    }
}
